﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Ewk.Runtime.Serialization.UnitTests.Records.CharacterSeparated;
using Ewk.Runtime.Serialization.UnitTests.Records.PositionBased;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Ewk.Runtime.Serialization;
using Ewk.Runtime.Serialization.UnitTests.Records;

namespace Ewk.Runtime.Serialization.UnitTests
{
    [TestClass]
    public class DatarecordSerializer_DateTimeTests
    {
        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void DatarecordSerializer_can_serialize_an_instance_of_a_type_with_a_CharacterSeparatedDatarecordAttribute_with_an_DateTime_field()
        {
            var date = DateTime.Now;
            var datarecord = new CharacterSeparatedTestDatarecordWithDateTimeField { Field = date };

            var serializer = new DatarecordSerializer<CharacterSeparatedTestDatarecordWithDateTimeField>();
            var result = serializer.Serialize(datarecord);

            Assert.AreEqual(date.ToString("dd-MM-yyyy"), result);
        }

        [TestMethod]
        public void DatarecordSerializer_can_deserialize_a_string_to_an_instance_of_a_type_with_a_CharacterSeparatedDatarecordAttribute_with_an_DateTime_field()
        {
            var date = DateTime.Now;

            var serializer = new DatarecordSerializer<CharacterSeparatedTestDatarecordWithDateTimeField>();
            var result = serializer.Deserialize(date.ToString("dd-MM-yyyy"));

            Assert.AreEqual(date.Date, result.Field.Date);
        }

        [TestMethod]
        public void DatarecordSerializer_serializes_an_instance_of_a_type_with_a_PositionBasedDatarecordAttribute_without_values_in_its_fields_to_a_string_with_the_default_character_of_the_PositionBasedDatarecordAttribute_if_no_default_characters_are_defined_on_the_fields()
        {
            var datarecord = new PositionBasedTestDatarecordWithDateTimeField();

            var serializer = new DatarecordSerializer<PositionBasedTestDatarecordWithDateTimeField>();
            var serializedRecord = serializer.Serialize(datarecord);
            
            Assert.AreEqual(new string(' ', 10), serializedRecord);
        }

        [TestMethod]
        public void DatarecordSerializer_can_serialize_an_instance_of_a_type_with_a_PositionBasedDatarecordAttribute_with_an_DateTime_field()
        {
            var date = DateTime.Now;
            var datarecord = new CharacterSeparatedTestDatarecordWithDateTimeField { Field = date };

            var serializer = new DatarecordSerializer<CharacterSeparatedTestDatarecordWithDateTimeField>();
            var serializedRecord = serializer.Serialize(datarecord);

            Assert.AreEqual(date.ToString("dd-MM-yyyy"), serializedRecord);
        }

        [TestMethod]
        public void DatarecordSerializer_can_deserialize_a_string_to_an_instance_of_a_type_with_a_PositionBasedDatarecordAttribute_with_an_DateTime_field()
        {
            var date = DateTime.Now;
            var serializedDatarecord = date.ToString("dd-MM-yyyy");

            var serializer = new DatarecordSerializer<PositionBasedTestDatarecordWithDateTimeField>();
            var deserializedRecord = serializer.Deserialize(serializedDatarecord);
            
            Assert.AreEqual(date.Date, deserializedRecord.Field.Date);
        }

        [TestMethod, ExpectedException(typeof(DatafieldSerializationException))]
        public void DatarecordSerializer_cannot_deserialize_a_string_containing_an_invalid_value_to_an_instance_of_a_type_with_a_PositionBasedDatarecordAttribute_with_an_DateTime_field()
        {
            const string serializedDatarecord = "xxxxxxxxxx";
            
            var serializer = new DatarecordSerializer<PositionBasedTestDatarecordWithDateTimeField>();
            serializer.Deserialize(serializedDatarecord);
        }
    }
}
